/**
 * 
 */
package com.sunbeam.common.junit;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Date;
import java.util.ArrayList;
import com.sunbeam.common.junit.accessories.UserInfoDao;
import com.sunbeam.common.junit.accessories.UserInfoEntity;
import com.sunbeam.common.utils.InvokeUtils;
import com.sunbeam.common.utils.MathUtils;
import com.sunbeam.common.utils.StringUtils;

/**
 * 
 * @ClassName: DBTest
 * @Description: TODO
 * @author AromaSunbeam lyzpoq_gmail_com
 * @date 2013-8-31 下午11:12:07
 * 
 */
public class DBTest {

	/**
	 * 
	 * @Title: main
	 * @Description: TODO
	 * @param @param args
	 * @return void
	 * @throws
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		UserInfoDao userInfoDao = new UserInfoDao();
		//Update Test
		UserInfoEntity userInfo=new UserInfoEntity();
		userInfo.setUID("UID20130828001");
		//userInfo.setPassword("Password");
		
		userInfoDao.UpdateTest(userInfo);
		System.out.println("*************After Update Data***************\r\n");
		ArrayList<UserInfoEntity> userInfoSels = (ArrayList<UserInfoEntity>) userInfoDao.SelectTest();
		//printUserInfos(userInfoSels);
		
		// SelectTest();
		
		userInfoSels = (ArrayList<UserInfoEntity>) userInfoDao.SelectTest();
		System.out.println("*************Before Delete Data***************\r\n");
		//printUserInfos(userInfoSels);
        
		userInfoDao.DeleteTest();
		userInfoSels = (ArrayList<UserInfoEntity>) userInfoDao.SelectTest();
		System.out.println("*************After Delete Data****************\r\n");
		//printUserInfos(userInfoSels);
		
		ArrayList<UserInfoEntity> userInfoInserts = createUserInfos(10);
		System.out.println("*****************Insert Data******************\r\n");
		//printUserInfos(userInfoInserts);

		long l1=System.currentTimeMillis();
		for (UserInfoEntity userInfoIns : userInfoInserts) {
			userInfoDao.InsertTest(userInfoIns);
		}
		long l2=System.currentTimeMillis();
		userInfoInserts = (ArrayList<UserInfoEntity>) userInfoDao.SelectTest();
		System.out.println("****************After Insert Data***************\r\n "+userInfoInserts.size());
		//printUserInfos(userInfoInserts);
		
		
        System.out.println((l2-l1)/1000.000+"s");
	}

	/**
	 * @Title: createUserInfos
	 * @Description: TODO
	 * @param @return
	 * @return ArrayList<UserInfoEntity>
	 * @throws
	 */
	protected static ArrayList<UserInfoEntity> createUserInfos(int count) {
		UserInfoEntity userInfoInsert = new UserInfoEntity();
		ArrayList<UserInfoEntity> userInfoInserts = new ArrayList<UserInfoEntity>();
		Field[] fields = InvokeUtils.getFields(userInfoInsert);
		Method[] methods = InvokeUtils.getMethods(userInfoInsert);
		for (int i = 0; i < count; i++) {
			userInfoInsert = new UserInfoEntity();
			for (int j = 0; j < fields.length; j++) {
				for (int k = 0; k < methods.length; k++) {
					Object[] obj = new Object[1];
					if (methods[k].getName().equalsIgnoreCase("set" + fields[j].getName())) {
						if (fields[j].getType().equals(String.class)) {
							if(!methods[k].getName().equalsIgnoreCase("setUID")
							 &&!methods[k].getName().equalsIgnoreCase("setPassword")){
								obj[0] = fields[j].getName()+ "_"+ StringUtils.getStringSequence(i + 1, MathUtils.GetIndices(10, count) + 1);
								InvokeUtils.execute(userInfoInsert,methods[k].getName(), obj);
							}
						} else if (fields[j].getType().equals(Date.class)) {
							obj[0] = new Date(System.currentTimeMillis() - (long) (Math.random() * 999999999999L));
							InvokeUtils.execute(userInfoInsert,methods[k].getName(), obj);
						}else if (fields[j].getType().equals(byte[].class)){
						}
					}
					else if(methods[k].getName().equalsIgnoreCase("set" + fields[j].getName()+"Str")){
						obj[0] = fields[j].getName()+ "_"+ StringUtils.getStringSequence(i + 1, MathUtils.GetIndices(10, count) + 1);
						InvokeUtils.execute(userInfoInsert,methods[k].getName(), obj);
					}
				}
			}
			userInfoInserts.add(userInfoInsert);
		}
		return userInfoInserts;
	}

	/**
	 * @Title: printUserInfos
	 * @Description: TODO
	 * @param @param userInfoSels
	 * @return void
	 * @throws
	 */
	protected static void printUserInfos(ArrayList<UserInfoEntity> userInfoSels) {
		for (UserInfoEntity userInfo : userInfoSels) {
			Method[] methods = InvokeUtils.getMethods(userInfo);
			Field[] fields = InvokeUtils.getFields(userInfo);
			for (Method method : methods) {
				for (Field field : fields) {
					if (method.getName().equalsIgnoreCase("get" + field.getName())) {
						if (field.getType().equals(String.class)) {
							System.out.print(InvokeUtils.execute(userInfo,method.getName(), new Object[] {})+ "\t");
						}else if (field.getType().equals(Date.class)) {
							System.out.print(InvokeUtils.execute(userInfo,method.getName(), new Object[] {})+ "\t");
						}if (field.getType().equals(byte[].class)){
							
						}
					}else if (method.getName().equalsIgnoreCase("get" + field.getName()+"Str")) {
						System.out.print(InvokeUtils.execute(userInfo,method.getName(), new Object[] {})+ "\t");
					}
				}
			}
			System.out.println();
		}
	}

}
